import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:toolbox/manager/hive/hiveHelper.dart';
import 'package:toolbox/page/browser/browser_state.dart';

import '../../../common/common.dart';
import '../../../widgets/defaultDialog.dart';

class AddHomepageUrlDialog extends StatefulWidget {
  AddHomepageUrlDialog({super.key, this.url});
  final String? url;

  @override
  State<AddHomepageUrlDialog> createState() => _AddHomepageUrlDialogState();
}

class _AddHomepageUrlDialogState extends State<AddHomepageUrlDialog> {
  TextEditingController controller = TextEditingController();

  bool get isEdit => widget.url != null;

  @override
  void initState() {
    super.initState();
    controller.text = widget.url ?? 'https://';
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  void back() {
    Get.back();
  }

  void _onTapSave() {
    String url = controller.text;
    if (url.isEmpty) {
      return;
    }
    if (isEdit) {
      HiveHelper.instance.deleteCustomHomepageUrl(widget.url ?? '');
      if (widget.url == HiveHelper.instance.webHomePageUrl) {
        HiveHelper.instance.webHomePageUrl = url;
      }
      HiveHelper.instance.setCustomHomepageUrl(url);
    } else {
      if (HiveHelper.instance.getAllCustomHomepageUrl().contains(url)) {
        BotToast.showText(text: '已存在');
        return;
      }
    }
    back();
  }

  void _onTapDelete() {
    Get.dialog(
      defaultDialog(
          title: '删除',
          content: widget.url,
          sureFunction: () {
            HiveHelper.instance.deleteCustomHomepageUrl(widget.url ?? '');
            if (widget.url == HiveHelper.instance.webHomePageUrl) {
              HiveHelper.instance.webHomePageUrl = bingUrl;
            }
            back();
          }
      ),
    );
  }

  @override
  Widget build(BuildContext context) {

    return GestureDetector(
      onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
      child: Scaffold(
        backgroundColor: Colors.transparent,
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Stack(
              children: [
                Container(
                  margin: const EdgeInsets.symmetric(horizontal: 20),
                  padding: const EdgeInsets.all(20),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(16),
                    color: Colors.white,
                  ),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children:[
                      TextFormField(
                        controller: controller,
                        autofocus: true,
                        decoration: const InputDecoration(
                          labelText: 'url',
                          hintText: 'https://'
                        ),
                      ),
                      const SizedBox(height: 16),
                      Row(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children:[
                            button('取消', back, false),
                            button(Strings.save, _onTapSave, true),
                          ]
                      ),
                    ],
                  ),
                ),
                Positioned(
                  right: 25,
                  top: 0,
                  child: IconButton(
                    onPressed: isEdit ? _onTapDelete : back,
                    icon: Icon(isEdit ? Icons.delete_outline : Icons.close),
                  ),
                )
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget button(String text, Function() onTap, bool active) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 40, vertical: 10),
        decoration: BoxDecoration(
            color: active ? Colors.blue : null,
            border: Border.all(color: Colors.blue),
            borderRadius: BorderRadius.circular(12)
        ),
        child: Text(
          text,
          style: Styles.textStyle(size: 16, color: active ? Colors.white : Colors.blue),
        ),
      ),
    );
  }
}
